const network = require('./network.js');
const InsertFormIds = `${network.SERVER_URL}/v1/WxSpTemplate/InsertFormIds`; // 用户登录
const bindTopUrl = `${network.SERVER_URL}/v1/User/BindSuperior`; // 绑定上级用户
class Common {
  sendFormId(id) {
    let FormIds = [];
    if ( "string" === typeof id ) {
      FormIds.push(id);
    } else {
      FormIds = id;
    }

    network.post(InsertFormIds, {FormIds})
  }
  deQrcode (options) {
    const scene = decodeURIComponent(options.scene);
    let option = {};
    if (scene !== 'undefined') {
      option =  this.cutOff(scene);
    } else {
      option = options;
    }
    return option;
  }
  cutOff (str) {
    const a = str.split('&');
    const option = {};
    for (var i = 0; i < a.length; i++) {
      const o = a[i].split('=');
      option[o[0]] = o[1];
    }
    return option;
  }
  // 绑定上线
  bindTop (uid) {
    if ( !uid ) {return false}
    wx.setStorageSync('user_id', uid);
    const user = JSON.parse(wx.getStorageSync('user') || '{}');
    if (!user.IsNewUser) {
      return false;
    }
    network.post(bindTopUrl, {UserPkId: uid}).then((data) => {
      console.log('绑定上线成功');
      const use = Object.assign(user, {IsNewUser: false});
      wx.setStorageSync('user', JSON.stringify(use));
    })
  }
}

exports.common = new Common();
